import time
from time import sleep

from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.common.by import By

####实例化浏览器
driver = webdriver.Edge()
#登录页
driver.get("https://mp.toutiao.com/auth/page/login")

# 等待扫码登录完成并跳转到首页
driver.implicitly_wait(1000)
print("登录成功")

# 获取浏览器session
# 获取浏览器Cookies
cookies = driver.get_cookies()
# 打印Cookies
for cookie in cookies:
    print(cookie)


# cookies = driver.get_s()
# # 打印Cookies
# for cookie in cookies:
#     print(cookie)


local_storage = driver.execute_script("return window.localStorage.getItem('xmst');")
print("Authorization--access_token:", str(local_storage))



driver.get("https://mp.toutiao.com/profile_v4/graphic/publish?from=toutiao_pc")

time.sleep(1)
title1 = driver.find_element(by=By.XPATH,
                            value="/html/body/div[1]/div/div[3]/section/main/div[2]/div/div/div[2]/div/div/div/div[1]/div[3]/div/div/div[2]/div/div/div/textarea")
print(title1)

title1.send_keys("我是标题")
time.sleep(1)

#填写内容
context1 = driver.find_element(by=By.XPATH, value="/html/body/div[1]/div/div[3]/section/main/div[2]/div/div/div[2]/div/div/div/div[1]/div[4]/div/div[1]")
print(context1)
context1.send_keys("<p data-track=\"1\">11111</p>")

time.sleep(1)
#是否单标题
rd = driver.find_element(by=By.XPATH, value="/html/body/div[1]/div/div[3]/section/main/div[2]/div/div/div[2]/div/div/div/div[2]/div[2]/div[1]/div[2]/div[1]/label[1]/span/span")
driver.execute_script("$(arguments[0]).click()",rd)

#无封面图片
no_pic = driver.find_element(by=By.XPATH, value="/html/body/div[1]/div/div[3]/section/main/div[2]/div/div/div[2]/div/div/div/div[2]/div[2]/div[2]/div/div[2]/div/div[1]/label[3]/span/span")
driver.execute_script("$(arguments[0]).click()",no_pic)

# #发布
# time.sleep(1)
# search_button = driver.find_element(by=By.XPATH,value="/html/body/div[1]/div/div[3]/section/main/div[2]/div/div/div[2]/div/div/div/div[3]/div/button[3]")
# driver.execute_script("$(arguments[0]).click()",search_button)
#
# #确认发布
# time.sleep(1)
# p_button = driver.find_element(by=By.XPATH,value='/html/body/div[1]/div/div[3]/section/main/div[2]/div/div/div[2]/div/div/div/div[3]/div/button[2]/span')
# driver.execute_script("$(arguments[0]).click()",p_button)
